import { error } from "cc";
import Monster from "./Monster";
import BaseEventMgr from "../../platform/event/BaseEventMgr";
import Hero from "./Hero";

export class BattleRoom extends BaseEventMgr{

    private static _currenRoom:BattleRoom = null
    private globalId = 1;

    static get currentRoom():BattleRoom{
        return BattleRoom._currenRoom
    }

    static newRoom(){
        BattleRoom._currenRoom = new BattleRoom()
    }

    //房间配置id
    roomId:number = 0;
    status:number = 0; //房间状态 0:未开始 1:进行中 2:暂停 3:结束

    //英雄
    hero:Hero = null;

    //召唤物

    //中立单位

    //怪物
    monsters:Monster[] = [];
    monsterMap:Map<number,Monster> = new Map();

    public addHero(hero:Hero){
        if(hero.uid==0){
            hero.uid = this.globalId++
        }
        if(!this.hero){
            this.hero = hero;
        }else{
            error("当前房间已经有英雄了")
        }
    }

    public addMonsters(monster:Monster){
        if(monster.id==0){
            monster.id = this.globalId++
        }
        if(!this.monsterMap.has(monster.id)){
            this.monsterMap.set(monster.id, monster)
            this.monsters.push(monster)
            monster.init()
        }
    }

    public removeMonster(param:number|Monster){
        let toDelete = null;
        if(typeof param == "number"){
            toDelete = this.monsterMap.get(param)
        }
        if(toDelete!=null){
            let index = this.monsters.indexOf(toDelete)
            this.monsters.splice(index,1)
            this.monsterMap.delete(toDelete.uid)
        }

    }

    //房间初始化
    public reset(roomId:number){
        this.roomId = roomId;
        //初始化房间的buff等，统计数据清理等,怪物清理等
    }
    //开始
    start(){
        this.status = 1; //运行中
    }
    //暂停
    //运行中
    update (dt: number) {
        //英雄行为
        this.heroBeheavior(dt);
        //TODO 英雄召唤物行为
        //TODO 英雄中立单位行为
        //怪物行为
        this.monsterBehavior(dt);

    }

    monsterBehavior(dt: number) {
        //移动
        this.monsters.forEach(monster => {
            monster.update(dt);
        } )
    }
    heroBeheavior(dt: number) {
        //移动

        //释放技能
    }
    //结束
}